package com.iretailer.report.web.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReFaceAlarm;
import com.iretailer.report.dto.ReFaceAlarmRecord;
import com.iretailer.report.model.*;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther Bennie
 * @Date 2020/6/3 14:17
 * @Description
 */
@RestController
public class FaceAlarmContoller extends BaseAction {

    @RequestMapping("/system/gridFaceAlarm.action")
    public Map<String, Object> gridFaceAlarm(@RequestBody ReFaceAlarm rfa, HttpServletRequest request) {
        QueryOption queryOption = modelMapper.map(rfa, QueryOption.class);
         User user = SecurityUserUtil.currentUser(request);
        String userId = user.getId();
        if (user.getRoleId().equals("-")) {
            userId = null;
        }
        PageResult<FaceAlarmMall> find = faceAlarmService.gridFaceAlarm(userId, rfa.getMallName(), rfa.getName(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/system/gridFaceAlarmRecord.action")
    public Map<String, Object> gridFaceAlarmRecord(@RequestBody ReFaceAlarmRecord rar) {
        QueryOption queryOption = modelMapper.map(rar, QueryOption.class);
        PageResult<FaceAlarmRecord> find = faceAlarmRecordService.gridFaceAlarmRecord(rar.getDid(), rar.getDate(), rar.getStatus(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/system/saveOrUpdateFaceAlarm.action")
    public Map<String, Object> saveOrUpdateFaceAlarm(@RequestBody ReFaceAlarm rfa) {
        FaceAlarm faceAlarm = modelMapper.map(rfa, FaceAlarm.class);
        boolean result = faceAlarmService.saveOrUpdateFaceAlarm(faceAlarm);
        return RMap.ok(result);
    }

    @RequestMapping("/system/deleteFaceAlarmByIds.action")
    public Map<String, Object> deleteFaceAlarmByIds(@RequestBody List<Long> ids) {
        boolean result = faceAlarmService.deleteFaceAlarmByIds(ids);
        if (result) {
            return RMap.ok();
        } else {
            return RMap.error(RMap.Code.CONNECTED, "associated_with_device");
        }
    }

    @RequestMapping("/system/faceAlarmSaveDevices.action")
    public Map<String, Object> faceAlarmSaveDevices(@RequestBody ReFaceAlarm rfa) {
        boolean result = faceAlarmService.saveFaceAlarmDevice(rfa.getId(), rfa.getDeviceIds());
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/system/getFaceDeviceAndFaceAlarmDevice.action")
    public Map<String, Object> getFaceDeviceAndFaceAlarmDevice(@RequestBody ReFaceAlarm rfa) {
        List<DeviceType> devices = devicesService.selectDeviceType(new QueryWrapper<DeviceType>().eq("MallId", rfa.getMallId()).eq("DeviceTypeName","FaceDevice"));
        List<Long> dids = faceAlarmService.selectDidsByFaceAlarmId(rfa.getId());
        return RMap.ok(new RMap().put("Devices", devices).put("Dids", dids));
    }

    @RequestMapping("/system/getFaceDeviceByMallId.action")
    public Map<String, Object> getFaceDeviceByMallId(@RequestBody ReFaceAlarm rfa) {
        List<DeviceType> devices = devicesService.selectDeviceType(new QueryWrapper<DeviceType>().eq("MallId", rfa.getMallId()).eq("DeviceTypeName","FaceDevice"));
        return RMap.ok(devices);
    }
}
